
#ifndef _base_h_
#define _base_h_
 
 
#include  <stdio.h>
#include  <stdlib.h>
 


#ifdef WIN32
	#include  <io.h>
	#include  <direct.h> 
	#include <string.h>
	#include <vector>
	#include <algorithm>
#else
	#include <dirent.h> 

	#include <sys/types.h> 
	#include <unistd.h>
	#include <pthread.h>
#endif 

#include <sys/stat.h>
 
#include <fcntl.h> /* Ϊ O_* ���� */


#include "commDef.h"
 


#include <string.h>

using namespace std;
 

#define  DIC_CMU_VERSION(x,y)  ( (x << 16) + y ) // MAKELONG 
#define  CURRENT_CMU_VERSION			   DIC_CMU_VERSION( 1,0 ) 

#define LPTHREAD_START_ROUTINE   void*(*)(void*)
#define	thread_type      pthread_t


namespace base
{
	unsigned int 	get_tick_count(); 
	void 		MySleep(int Millisecond);
	
	bool 		GetLocalIP2(std::vector<string> & aryIP); 
	string 		GetCurrentProcessName();
	string 		GetCurrentProcessPath();
	bool        KillProcessFromName(char* szProcessName);

	bool 		is_dir_exit(const char * dir_path);
	bool 		is_exist_file(const char *path);
	int 		copy_file(char * srcProgram, char *newProgram);
	void 		read_file(string dirname, std::vector<string> & aryfiles);
	int 		HStatFileSize(const char *pszFileName);
	void 		HMkDir(const char * path);
	void 		get_file_list(const char* spec_filter,std::vector<std::string> & lst) ;
	
	void		my_printf(const char * format, ...);
	void 		MyLog(unsigned char level, const char * format, ...);

	bool		StringSplit_ByChar( const char * split , const char* source, char * left, char * right );

	/////////////////////
	char * 		l_trim(char * szOutput, const char *szInput);    /*   ɾ����ߵĿո�   */  
	void *		r_trim(char *szOutput, const char *szInput) ;      /*   ɾ���ұߵĿո�   */ 
	char * 		a_trim(char * szOutput, const char * szInput) ;   /*   ɾ�����ߵĿո�   */ 

	char* 		itoa(int  value, char *string, int radix);
	void 		string_replace( string & s1, const string & s2, const string & s3);
	int 		IsNumeric(const char* ccharptr_CharacterList);
	int 		strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive);
	int 		strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive);
	
	void 		MakeUpper(std::string& text); 
	void 		MakeLower(std::string& text); 
	void 		TrimLeft(std::string& text); 
	void 		TrimRight(std::string& text); 
	void 		Trim(std::string& text); 
	int  		Replace(std::string& text, char oldChar, char newChar); 
	int  		Replace(std::string& text, const char* oldString, const char* newString);

	string  	toTimeString(time_t tt, bool IsBjTime=false);
	int 		TimeFormatToInt( char * pTimeFormat, bool IsBjTime=false );
	
#ifdef _WIN32
	string		Utf8ToGbk_ex(const char *src_str);
	string		GbkToUtf8_ex(const char *src_str);  
#endif
	int			GbkToUtf8(char *src_str, size_t src_len, char *dst_str, size_t dst_len);
	int			Utf8ToGbk(char *src_str, size_t src_len, char *dst_str, size_t dst_len);

	/* 
	[cls_server] 
	#�����ļ��Ⱥ����ҿ����пո�Ҳ����û�� 
	ip=192.16.31.2
	*/ 
	int 		GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal ) ; 
	int 		WriteProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal );
	
	//
	unsigned char	bcd_decimal(unsigned char bcd);
	unsigned char	decimal_bcd(unsigned char decimal);
	void		bcd_to_decimal(char * bcd_code, int bcd_len, char* decimalBuf);

#ifndef WIN32 	 
	pid_t 		GetPIDbyName_Wrapper(const char* cchrptr_ProcessName);
	size_t 		get_executable_path( char* processdir,char* processname, size_t len);
	bool 		getPathNamebyPid(pid_t pid,char *name); 
	bool 		is_existed_pid(char * chrCurPidName);
	
	thread_type    Thread_start ( 
		  			void* (*lpStartAddress)(void*)  ,
		  			void*  ThreadParameter 
				    );
	
#endif 
  
};	

struct  Server_info
 {
	 char ipAddr[64];
	 int  iPort;
 }; 
namespace XSysHelper
{
	string  	DIC_VERSION_NAME( int version );
	int  		CMU_VER_NAME_TO_ID( string strVersion );
	
	int 		Read_Cmu_Version();
	void        Write_Cmu_Version(int cur_cmu_version);
	string 		LoadCmuNo();
	bool 		LoadSyncModuleServerInfo(struct Server_info * pSvrInfo);
	string 		LoadCmuHostIp();
	bool 		LoadDevModelServerInfo(struct Server_info * pSvrInfo);
	int 		LoadScadaSvrPort();
	bool        IsHost(); 
}; 

#endif
